﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Lottery.Herper;
using Lottery.MediaData.Core;

namespace Lottery.MediaData.UI {
    public partial class SelectSubIndexFrom14 : Form {
        private SelectSubIndexFrom14() {
            InitializeComponent();
        }

        private string m_qiShu;
        private string m_tableName;
        private string m_ccString;
        private string m_defaultSubIndex;
        private int m_maxCount;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="qiShu">期数</param>
        /// <param name="tableName">表: T1 T2 T3 T4</param>
        /// <param name="cc">场次</param>
        public SelectSubIndexFrom14(string qiShu, string tableName, string cc, string defaultSubIndex, int maxCount): this() {
            m_qiShu = qiShu;
            m_tableName = tableName;
            m_ccString = cc;
            m_defaultSubIndex = defaultSubIndex;
            m_maxCount = maxCount;

            this.SetDialogStyle();
            labelInfo.Text = string.Format("期数: {0} 的序号", qiShu);

            var subIndex = DataHelper.GetSubIndexes(tableName, qiShu, string.Empty, 14);
            listBoxSubIndex.Items.Clear();
            listBoxSubIndex.Items.AddRange(subIndex);
            listBoxSubIndex.SelectedIndex = listBoxSubIndex.Items.IndexOf(defaultSubIndex);
        }

        private void button2_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
        }

        private void button1_Click(object sender, EventArgs e) {
            if (listBoxSubIndex.Items.Count == 0) {
                this.ShowInfo("没有序号可以选择");
                return;
            }
            if (listBoxSubIndex.SelectedIndex == -1) {
                this.ShowInfo("请选择序号");
                return;
            } 
            var subIndex = listBoxSubIndex.Items[listBoxSubIndex.SelectedIndex].ToString();

            var info = string.Format("请确认是否复制序号 {0} 的数据?", subIndex);
            if (this.ShowYesNoInfo(info) != DialogResult.Yes) {
                return;
            }
            if (m_tableName == "T1") {
                DataHelper.CopyDataFrom14ToT1(m_qiShu, m_ccString, m_maxCount, subIndex);
            } else if (m_tableName == "T2") {
                DataHelper.CopyDataFrom14ToT2(m_qiShu, m_ccString, m_maxCount, subIndex);
            } else if (m_tableName == "T3") {
                DataHelper.CopyDataFrom14ToT3(m_qiShu, m_ccString, m_maxCount, subIndex);
            } else if (m_tableName == "T4") {
                DataHelper.CopyDataFrom14ToT4(m_qiShu, m_ccString, m_maxCount, subIndex);
            }
            DialogResult = DialogResult.OK;
        }

        private void button3_Click(object sender, EventArgs e) {
            if (listBoxSubIndex.Items.Count == 0) {
                this.ShowInfo("没有序号可以选择");
                return;
            }
            var info = string.Format("请确认是否复制全部序号的数据?");
            if (this.ShowYesNoInfo(info) != DialogResult.Yes) {
                return;
            }

            for (int i = 0; i < listBoxSubIndex.Items.Count; i++) {
                var subIndex = listBoxSubIndex.Items[i].ToString();

                if (m_tableName == "T1") {
                    DataHelper.CopyDataFrom14ToT1(m_qiShu, m_ccString, m_maxCount, subIndex);
                } else if (m_tableName == "T2") {
                    DataHelper.CopyDataFrom14ToT2(m_qiShu, m_ccString, m_maxCount, subIndex);
                } else if (m_tableName == "T3") {
                    DataHelper.CopyDataFrom14ToT3(m_qiShu, m_ccString, m_maxCount, subIndex);
                } else if (m_tableName == "T4") {
                    DataHelper.CopyDataFrom14ToT4(m_qiShu, m_ccString, m_maxCount, subIndex);
                }
            }
            DialogResult = DialogResult.OK;
        }
    }
}
